package com.honey.mybatis.generator;

import org.java.plugin.Plugin;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.InnerClass;
import com.honey.compilation.java.InnerEnum;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.StaticBlock;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.ContentType;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.generator.java.extension.point.GeneratorJavaClass;

/**
 * mybatis 实体
 * @author Administrator
 * 
 */
public class MybatisEntityExtension extends AbstractMybatisExtension implements GeneratorJavaClass{

	private AbstractProduceJavaCompilation abstractProduceJavaCompilation;
	
	private AbstractJavaCompilation abstractJavaCompilation = null;
	
	public MybatisEntityExtension(Plugin belongPlugin) {
		super(belongPlugin);
	}
	
	@Override
	public AbstractJavaCompilation createNewCompilation(IntrospectSchema introspectSchema, IntrospectSchemaColumn introspectSchemaColumn) {

		abstractProduceJavaCompilation = MybatisFactory.getAbstractProduceJavaCompilation(MybatisFactory.MybatisClass.ENTITY_CLASS, mybatisPlugin, introspectSchema, introspectSchemaColumn);
		
		try {
			abstractJavaCompilation= abstractProduceJavaCompilation.produceCompilation();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return abstractJavaCompilation;
	}
	
	@Override
	public FullyQualifiedJavaType getType() {
		return abstractJavaCompilation.getType();
	}
	
	@Override
	public Field[] importFields() {
//		return (new CreateFields(mybatisPlugin)).createField(introspectSchema, introspectSchemaColumn);
		Field[] answer = null;
		try {
			answer= abstractProduceJavaCompilation.produceFields();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return answer;
	}

	@Override
	public InnerEnum[] importInnerEnumClass() {
//		List<InnerEnum> answer = new ArrayList<InnerEnum>();
//		List<Column> columns = introspectSchemaColumn.allColumns();
//		for(Column column : columns ){
//			if( column.getEnumSchema() != null ){
//				answer.add(
//					(InnerEnum) (new CreateInnerEnum(mybatisPlugin,column.getEnumSchema())).createAbstractJavaCompilation(introspectSchema, introspectSchemaColumn)
//				);
//			}
//		}
//		return answer.toArray(new InnerEnum[answer.size()]);
		return null;
	}

	@Override
	public Method[] importMethods() {
//		List<Column> columns = introspectSchemaColumn.allColumns();
//		Method[] answer = new Method[introspectSchemaColumn.allColumns().size() * 2 + 3];
//		answer[0] =(new CreateDefaultConstructorMethod(mybatisPlugin)).createMethod(clazz, null, null, null) ;//默认的构造函数
//		answer[1] = (new CreateConstructorMethod(mybatisPlugin)).createMethod(clazz, null, null, introspectSchemaColumn);//带有参数构造函数
//		int methodIndex = 2 ;	
//		for(Column column : columns ){
//			answer[methodIndex] = (new CreateGetterMethod(mybatisPlugin)).createMethod(clazz, column, introspectSchema, introspectSchemaColumn);
//			answer[++methodIndex]= (new CreateSetterMethod(mybatisPlugin)).createMethod(clazz, column, introspectSchema, introspectSchemaColumn);
//			methodIndex++;
//		}
//		if(mybatisPlugin.isEntityOverrideToString() ){
//			answer[methodIndex] =  (new CreateToStringMethod(mybatisPlugin)).createMethod(clazz, null, introspectSchema, introspectSchemaColumn);
//		}
//		return answer;
		Method[] answer = null;
		try {
			answer= abstractProduceJavaCompilation.produceMethods();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return answer;
	}

	@Override
	public StaticBlock[] importStaticBlocks() {
		return null;
	}

	@Override
	public FullyQualifiedJavaType[] importImportClass() {
		return null;
	}

	@Override
	public InnerClass[] importInnerClass() {
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#getContentType()
	 */
	@Override
	public ContentType getContentType() {
		
		return ContentType.JAVA_ENTITY_BEEN;
	}
}
